package com.sundi.ecsystem.web;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sundi.eccommon.dto.EcStorageDto;
import com.sundi.eccommon.vo.ResultEntity;
import com.sundi.ecsystem.entity.EcStorage;
import com.sundi.ecsystem.service.IEcStorageService;
import com.sundi.ecsystem.utils.UploadUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 文件存储表 前端控制器
 * </p>
 *
 * @author Mht
 * @since 2020-03-01
 */
@RestController
@RequestMapping("/ecStorage")
@Slf4j
public class EcStorageController {

    @Autowired
    IEcStorageService storageService;

    @Autowired
    UploadUtils uploadUtils;

    @RequestMapping("storList")
    public ResultEntity list(EcStorageDto ecStorageDto){
        PageHelper.startPage(ecStorageDto.getPageNum(),ecStorageDto.getPageSize());
        Wrapper<EcStorage> wrapper = new EntityWrapper<EcStorage>();
        wrapper.eq("deleted",0);
        if(ecStorageDto.getKey()!=null){
            wrapper.like("key",ecStorageDto.getKey());
        }
        if(ecStorageDto.getType()!=null){
            wrapper.like("name",ecStorageDto.getName());
        }
        List<EcStorage> ecStorages = storageService.selectList(wrapper);
        PageInfo<EcStorage> pageInfo  = new PageInfo<>(ecStorages);
        return ResultEntity.ok(pageInfo);
    }

    @RequestMapping("addStor")
    public ResultEntity addStor(@ModelAttribute EcStorageDto ecStorageDto) throws IOException {
        List<String> upload = uploadUtils.upload(ecStorageDto.getFiles());
        log.info("图片地址是:{}",upload);
        if(!upload.isEmpty()){
            ecStorageDto.setUrl(upload.get(0));
        }
        ecStorageDto.setFiles(null);
        EcStorage ecStorage = new EcStorage();
        BeanUtils.copyProperties(ecStorageDto,ecStorage);
        storageService.insert(ecStorage);
        return ResultEntity.ok("添加成功");
    }

    @RequestMapping(value = "updStor",method = RequestMethod.PUT)
    public ResultEntity updStor(@ModelAttribute EcStorageDto ecStorageDto) throws IOException {
        log.info("传入参数1为:{}",ecStorageDto);
        if(ecStorageDto.getImages()!=null){
            ecStorageDto.setUrl(ecStorageDto.getImages()[0]);
            ecStorageDto.setFiles(null);
        }else {
            List<String> upload = uploadUtils.upload(ecStorageDto.getFiles());
            log.info("图片地址是:{}",upload);
            if(!upload.isEmpty()){
                ecStorageDto.setUrl(upload.get(0));
            }
            ecStorageDto.setFiles(null);
        }
        ecStorageDto.setImages(null);
        EcStorage ecStorage = new EcStorage();
        BeanUtils.copyProperties(ecStorageDto,ecStorage);

        Wrapper<EcStorage> wrapper = new EntityWrapper<EcStorage>();
        wrapper.eq("id",ecStorage.getId());
        storageService.update(ecStorage,wrapper);
        return ResultEntity.ok("修改成功");
    }
    @RequestMapping("delStor")
    private ResultEntity delStor(@RequestBody EcStorageDto ecStorageDto){

        Wrapper<EcStorage> wrapper = new EntityWrapper<EcStorage>();
        wrapper.eq("id",ecStorageDto.getId());

        EcStorage ecStorage = new EcStorage();
        BeanUtils.copyProperties(ecStorageDto,ecStorage);
        ecStorage.setDeleted(1);

        storageService.update(ecStorage,wrapper);

        return ResultEntity.ok("逻辑删除成功");
    }



}
